#!/bin/bash


function on_off () {
  if [ $# == 0 ]
  then
    echo ON
  fi
  par=$(echo $1 | tr '[:upper:]' '[:lower:]')
  case $1 in
    on*)
      echo ON
      ;;
    off*)
      echo OFF
      ;;
    en*)
      echo ON
      ;;
    dis*)
      echo OFF
      ;;
    *)
      echo ON
   esac;
}

function nox () {
  echo -D Trilinos_ENABLE_NOX:BOOL=$1;
}

function thyra () {
  echo -D Trilinos_ENABLE_Thyra:BOOL=$1;
}

function ml () {
  echo -D Trilinos_ENABLE_ML:BOOL=$1;
}

function rythmos () {
  echo -D Trilinos_ENABLE_Rythmos:BOOL=$1;
}

function indices32 () {
  echo -D Trilinos_NO_64BIT_GLOBAL_INDICES:BOOL=$1;
}

function mpi () {
  echo -D TPL_ENABLE_MPI:BOOL=$1;
}

function zoltan () {
  echo -D Trilinos_ENABLE_Zoltan:BOOL=$1;
}

function boost () {
  echo -D TPL_ENABLE_Boost:BOOL=$1;
}

function read_options () {
  # read current configuraiton
  opts=$(adapt query self opts)
  while [ $# -gt 0 ]
  do
    par=$1
    opt=${par%%=*}
    val=${par##*=}
echo $opt $val
    # normalize val
    val=$(on_off $val)
    # generate options only for new options
    if [[ "$opts" != *$opt=$val* ]]
    then
      opts=$opt=$val:$opts
      new_options=yes
    fi
    shift
  done
  adapt register self opts="'$opts'"
}

function options () {
  # options in opts should be sep by :
  for opt in $(echo $opts | tr : ' ')
  do
    key=${opt%%=*}
    val=${opt##*=}
    echo -n $($key $val)" " # space after eval is important!
  done
}

read_options $@
transf_opts=$(options)

# if it is installed, some options must be given and they must be different
# so if it is installed, new options must be given
if [ x$(adapt query self installed) == xyes -a ${new_options:-no} == no ]
then
  # deployment was invoked but it occured that requested options were already applied so 
  # nothing else could be done
  echo warunki na wyjscie
  exit 1
else
  echo mozna kontynuowac
fi

cd $(adapt query self bld)

# clean installation before changes, also remove cmake cache as it may have sth implicitly set
rm -rf CMake*
<install_manifest.txt xargs -L1 rm -rf

# MPI_ENABLED must be on, otherwise zoltan freaks out
# in serial mode they copy mpi.h to include without checking if 
# the org. mpi.h file exists or not
adapt cmake \
  -D CMAKE_BUILD_TYPE:STRING=RELEASE \
  -D CMAKE_INSTALL_PREFIX:STRING=$(adapt query sys/prefix) \
  -D TPL_ENABLE_BLAS:BOOL=ON \
  -D TPL_ENABLE_LAPACK:BOOL=ON \
  -D TPL_ENABLE_MPI:BOOL=ON \
  $transf_opts ..

#  -D BLAS_LIBRARY_DIRS=$(adapt query lib/blas lib) \
#  -D BLAS_LIBRARY_NAMES=$(adapt query lib/blas libs) \

#  -D LAPACK_LIBRARY_DIRS=$(adapt query lib/lapack lib) \
#  -D LAPACK_LIBRARY_NAMES=$(adapt query lib/lapack libs) \

echo adapt cmake \
  -D CMAKE_BUILD_TYPE:STRING=RELEASE \
  -D CMAKE_INSTALL_PREFIX:STRING=$(adapt query sys/prefix) \
  -D TPL_ENABLE_MPI:BOOL=ON \
  -D TPL_ENABLE_BLAS:BOOL=ON \
  -D TPL_ENABLE_LAPACK:BOOL=ON \
  -D TPL_ENABLE_HDF5:BOOL=ON \
  -D TPL_ENABLE_ParMETIS:BOOL=ON \
  -D ParMETIS_LIBRARY_NAMES:STRING=parmetis \
  -D TPL_ENABLE_UMFPACK:BOOL=ON \
  -D TPL_ENABLE_AMD:BOOL=ON \
  -D TPL_ENABLE_Boost:BOOL=ON \
  -D TPL_ENABLE_Zlib:BOOL=ON \
  -D TPL_ENABLE_Pthread:BOOL=ON \
  -D Trilinos_ENABLE_Teuchos:BOOL=ON \
  -D Trilinos_ENABLE_Epetra:BOOL=ON \
  -D Trilinos_ENABLE_EpetraExt:BOOL=ON \
  -D Trilinos_ENABLE_Zoltan:BOOL=ON \
  -D Trilinos_ENABLE_AztecOO:BOOL=ON \
  -D Trilinos_ENABLE_Belos:BOOL=ON \
  -D Trilinos_ENABLE_Anasazi:BOOL=ON \
  -D Trilinos_ENABLE_Amesos:BOOL=ON \
  -D Trilinos_ENABLE_Ifpack:BOOL=ON \
  -D Trilinos_ENABLE_ML:BOOL=ON \
  -D Trilinos_ENABLE_NOX:BOOL=ON \
  -D Trilinos_ENABLE_Rythmos:BOOL=ON \
  -D Trilinos_ENABLE_Thyra:BOOL=ON \
  -D Trilinos_NO_64BIT_GLOBAL_INDICES:BOOL=ON \
  $src

adapt make -j $(adapt query sys/cpunum)

adapt make install
